<%@ page language="java" contentType="text/html;charset=UTF-8" pageEncoding="UTF-8" %>
<%@page import="java.sql.Connection"%>
<%@page import="com.yuxin.common.util.DBUtil"%>
<%@page import="java.util.List"%>
<%@page import="java.util.Map"%>
<%@page import="java.net.URLDecoder"%>
<%@page import="com.yuxin.common.util.CommonUtil"%>
<%
	Connection conn = null;
	Map<String, Object> infoMap = null;
	String redirect = null;
	TransactionManager tranM = null;
	String action = request.getParameter("action");
	try {		
		 if(!CommonUtil.isLoginValid(request)){
			 response.sendRedirect(request.getContextPath()+"/site/denglu.jsp");
			 return;
		 }
		String credit_user_id = CommonUtil.getCookie(request, "TICKET");
		conn = DBUtil.getConnection();
		 // 页面标题
        request.setAttribute("titleName",URLDecoder.decode("个人中心", "UTF-8"));
		
		String sql = "select t.ID, t.NICKNAME, t.SEX, DATE_FORMAT(t.DOB, '%Y-%m-%d') as DOB, t.CREDIT_NUM from p_credit_user t where t.ID = ?";
		infoMap = DBUtil.queryUniqueResult(conn, sql, credit_user_id);
		
		if(action != null && "submit".equals(action)) {
			tranM = new TransactionManager(conn);
			tranM.begin();
			
			sql = "update p_credit_user set CREDIT_NUM = ?, NICKNAME = ?, SEX = ?, DOB = ? where ID = ?";
			String creditnum = request.getParameter("creditnum");
			String nickname = request.getParameter("nickname");
			String sex = request.getParameter("sex");
			String dob = request.getParameter("dob");
			DBUtil.executeSql(conn, sql, new Object[]{creditnum, nickname, sex, new SimpleDateFormat("yyyy-MM-dd").parse(dob), credit_user_id});
			
			redirect = request.getContextPath()+"/site/my_info.jsp";
		}
	} catch (Exception ex) {
		if(tranM != null) {
			tranM.rollback();
		}
		ex.printStackTrace();
	} finally {
		if(tranM != null) {
			tranM.commit();
		}
		DBUtil.close(conn);
		if(redirect != null) {
			response.sendRedirect(redirect);
		}
	}
%>
<!DOCTYPE html>

<%@page import="com.yuxin.core.TransactionManager"%>
<%@page import="java.text.SimpleDateFormat"%><html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
		<title>${titleName}</title>
        <meta name="Keywords" content="中信信用卡平台" />
        <meta name="Description" content="${titleName}"/>
        <meta name="robots" content="all"/>
        <meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
        <meta name="apple-mobile-web-app-capable" content="yes"/>
        <meta name="apple-mobile-web-app-status-bar-style" content="black"/>
        <meta name="format-detection" content="telephone=no"/>
        <meta http-equiv="cleartype" content="on"/>
        <link rel="icon" type="image/png" href="images/favicon.ico">
        <%@ include file="/site/css.jsp" %>
        <style type="text/css">
			.biaoge{margin-top:10px;width:100%;background-color:#fff;overflow:hidden;border:1px solid #C6C4C2;line-height:24px;-webkit-border-radius: .2em;-moz-border-radius: .2em;border-radius: .2em;}
			.biaoge li{list-style-type: none;color:#666;padding:1px 10px;border-bottom:1px solid #C6C4C2;line-height:30px;overflow:hidden;}
			.biaoge li:last-child{border-bottom: none;}
			.biaoge .name{float:left;}
			.biaoge .value{float:right;}
			.biaoge .edit{width:100%; float:left; margin-bottom:5px;}
		</style>
	</head>
	<body>
        <div id="page">
            	<%@ include file="/site/header_nav.jsp" %>
	            <div id="container"> 
	            	<form id="infoForm" name="infoForm" action="my_info.jsp?action=submit" method="post">  
                    	<ul class="biaoge">
                        <li>
                            <div class="name">信用卡号</div>
                            <div class="value">
								<%=!"edit".equals(action) ?  infoMap.get("CREDIT_NUM"): "" %>                                
                            </div>
							<%
                                if("edit".equals(action)) {							
                            %>
                            <div class="edit">
                            <input  id="creditnum" name="creditnum" placeholder="请输入信用卡号" class="text" autocomplete="0" style="width:90%" 
                            	value="<%=infoMap.get("CREDIT_NUM") %>" />
                            </div>
                            <%
                                }
                            %>
                        </li>
                        <li>
                            <div class="name">昵称</div>
                            <div class="value">
								<%=!"edit".equals(action) ?  infoMap.get("NICKNAME"): "" %>                                
                            </div>
							<%
                                if("edit".equals(action)) {							
                            %>
                            <div class="edit">
                            <input  id="nickname" name="nickname" placeholder="请输入昵称" class="text" autocomplete="0" style="width:90%" value="<%=infoMap.get("NICKNAME") %>" />
                            </div>
                            <%
                                }
                            %>
                        </li>
                        <li>
                            <div class="name">性别</div>
                            <div class="value"><%=!"edit".equals(action) ?  ("1".equals(infoMap.get("SEX")) ? "男" : "女"): "" %></div>                           
							<%
					   			if("edit".equals(action)) {							
							%>
                            <div class="edit">
							 <input name="sex" type="radio" value="1" <%=("1".equals(infoMap.get("SEX")) ? "checked" : "")%> >男 
							 &nbsp;&nbsp;
							 <input name="sex" type="radio" value="2" <%=("2".equals(infoMap.get("SEX")) ? "checked" : "")%> >女
                             </div>
							<%
								}
							%>							
                        </li>
                        <li>
                            <div class="name">出生日期</div>
                            <div class="value"><%=!"edit".equals(action) ?  infoMap.get("DOB"): "" %></div>
                            <%
					   			if("edit".equals(action)) {							
							%>
                            <div class="edit">
							 <input id="dob" name="dob" placeholder="请选择出生日期" type="date" class="text" autocomplete="0" style="width:90%" value="<%=infoMap.get("DOB") %>" />
                            </div> 
							<%
								}
							%>
                        </li>
                    </ul>        	            	
		             
					<%
                        if("edit".equals(action)) {							
                    %>
                    <div class="box-btn clearfix">
                        <a href="#" onclick="doSubmit();return false;" class="button big green">提交</a>
                    </div>
                     <%
                        }
                    %>  
                 </form>
                 <ul id="part" class="part1 g-F9F9F9-DEDEDE-FFFFFF-CFCFCF" style="background:none repeat scroll 0 0 #fff; margin-top:50px;">
                    <li>
                        <a href="my_order.jsp" style="font-weight: normal;">
                            <img src="${ctx}/site/images/icon/qt/qita_41.png" align='absmiddle'/>
                            <span class="sec-name">我的订单</span>
                        </a>
                    </li>
                    <li>
                        <a href="my_favorite.jsp" style="font-weight: normal;">
                            <img src="${ctx}/site/images/icon/qt/qita_42.png" align='absmiddle'/>
                            <span class="sec-name">我的收藏</span>
                        </a>
                    </li>
                </ul>
                
                <ul id="part" class="part1 g-F9F9F9-DEDEDE-FFFFFF-CFCFCF" style="background:none repeat scroll 0 0 #fff; margin-top:50px;">
                    <li>
                        <a href="my_info.jsp?action=edit" style="font-weight: normal;">
                            <img src="${ctx}/site/images/icon/sr.png" align='absmiddle'/>
                            <span class="sec-name">个人信息</span>
                        </a>
                    </li>
                </ul>
                
                <div class="box-btn clearfix" style="margin-top:30px;">
                    <a href="dengchu.jsp" class="button big red">退出</a>
                </div>
            </div>
            <%@ include file="/site/footer.jsp" %>
        </div>
    </body>
    
    <%@ include file="/site/js.jsp" %>
	<script src="${ctx}/site/js/jquery.form.js"></script>
    <script>
        function doSubmit(){
			if($("#creditnum").val().length < 1){
                appUtil.alert("请输入信用卡号！");
                $("#creditnum").focus();
                return;
            }
            if($("#nickname").val().length < 1){
                appUtil.alert("请输入昵称！");
                $("#nickname").focus();
                return;
            }
            if($("#dob").val().length < 1){
                appUtil.alert("请选择出生日期！");
                $("#dob").focus();
                return;
            }
            $('#infoForm').submit();
        }
    </script>
</html>